package de.preamp.clients.java.utils.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import de.preamp.clients.java.lib.ui.AbstractLibraryUI;
import de.preamp.clients.java.lib.ui.LibraryUIFancy;
import de.preamp.clients.java.player.ui.ControlsUI;
import de.preamp.clients.java.playlist.ui.PlaylistManagerUI;
import de.preamp.clients.java.utils.ImageLoader;
import de.preamp.clients.java.utils.Tools;

/**
 * The top level window for the GUI.
 * 
 * @author André Stannek
 * @version 2010-06-11
 */
public class MainUI extends JFrame
{

    /**
     * generated UID.
     */
    private static final long serialVersionUID = -1958209249992533567L;

    /**
     * reference variable for use in ActionListeners.
     */
    private final transient MainUI mainRef = this;

    /**
     * 
     */
    public MainUI()
    {
	super("preamp");

	initUI();
    }

    /**
     * 
     */
    private void initUI()
    {
	final int width = 1024;
	final int height = 786;

	this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	this.setIconImage(Toolkit.getDefaultToolkit().getImage(
		ImageLoader.getImageURL("audio-x-generic.png")));
	final JPanel contentPane = new JPanel();
	contentPane.setLayout(new BorderLayout());

	// split pane in the middle
	final JSplitPane splitPane =
		new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	splitPane.setOneTouchExpandable(true);
	splitPane.setDividerLocation(150);

	final Dimension minimumSize = new Dimension(100, 150);

	final AbstractLibraryUI libraryUI = new LibraryUIFancy();

	final PlaylistManagerUI playlistPanel = new PlaylistManagerUI();
	playlistPanel.setMinimumSize(minimumSize);
	splitPane.setLeftComponent(playlistPanel);
	splitPane.setRightComponent(libraryUI);
	libraryUI.setMinimumSize(minimumSize);

	// controls
	final JPanel controls = new ControlsUI();

	// menue bar
	final JMenuBar menuBar = new JMenuBar();

	final JMenu file = new JMenu("Preamp");

	final JMenuItem prefs = new JMenuItem("preferences", KeyEvent.VK_P);
	prefs.setIcon(new ImageIcon(ImageLoader
		.getImageURL("preferences-system.png")));
	prefs.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(final ActionEvent event)
	    {
		new PreferencesUI(mainRef);
	    }
	});
	file.add(prefs);

	final JMenuItem close = new JMenuItem("quit", KeyEvent.VK_Q);
	close.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(final ActionEvent event)
	    {
		System.exit(0);
	    }
	});
	file.add(close);

	final JMenu lib = new JMenu("Library");
	final JMenuItem scan = new JMenuItem("scan library", KeyEvent.VK_S);
	scan.setIcon(new ImageIcon("icons/view-refresh.png"));
	scan.getAccessibleContext().setAccessibleDescription(
		"Causes the server to rescan the library.");
	scan.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(final ActionEvent event)
	    {
		new ScanDialog(mainRef);
	    }
	});
	lib.add(scan);

	final JMenu helpMenu = new JMenu("Help");
	final JMenuItem help = new JMenuItem("Help", KeyEvent.VK_H);
	help.setIcon(new ImageIcon(ImageLoader.getImageURL("help-browser.png")));
	helpMenu.add(help);
	final JMenuItem about = new JMenuItem("About", KeyEvent.VK_A);
	about.addActionListener(new ActionListener()
	{
	    @Override
	    public void actionPerformed(final ActionEvent event)
	    {
		new AboutDialog(mainRef);
	    }
	});
	helpMenu.add(about);

	menuBar.add(file);
	menuBar.add(lib);
	menuBar.add(helpMenu);

	// add
	contentPane.add(splitPane, BorderLayout.CENTER);
	contentPane.add(controls, BorderLayout.SOUTH);
	this.setJMenuBar(menuBar);
	this.setContentPane(contentPane);
	this.setBounds(Tools.getCenteredBounds(width, height));
    }
}
